package com.example.hotelsystem.controller.room;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.hotelsystem.entity.RoomType;
import com.example.hotelsystem.entity.RoomTypeImage;
import com.example.hotelsystem.entity.TDict;
import com.example.hotelsystem.entity.User;
import com.example.hotelsystem.result.Result;
import com.example.hotelsystem.service.RoomTypeImageService;
import com.example.hotelsystem.utils.UserUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.ss.formula.functions.T;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Lee
 * @since 2021-03-28
 */
@RestController
@RequestMapping("/hotelsystem/roomTypeImage")
@Api(tags = "房间类型图片控制器")
public class RoomTypeImageController {
    @Autowired
    private RoomTypeImageService roomTypeImageService;

    @PostMapping("/imageList")
    @ApiOperation("根据房间类型ID获取相应的图片列表")
    public Result getImageList(Long roomTypeId,Long type){
        try {
            QueryWrapper<RoomTypeImage> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("room_type_id",roomTypeId);
            if(type!=null){
                queryWrapper.eq("type",type);
            }
            List<RoomTypeImage> list = roomTypeImageService.list(queryWrapper);
            if(list!=null&&list.size()>0){
                return Result.success(list);
            }
            return Result.error("图片列表为空");
        }catch (Exception e){
            return Result.error(e.getMessage());
        }
    }

    @PostMapping("/getUrlList")
    @ApiOperation("根据房间类型ID获取相应的图片列表")
    public Result getUrlList(Long roomTypeId,Long type){
        try {
            QueryWrapper<RoomTypeImage> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("room_type_id",roomTypeId);
            if(type!=null){
                queryWrapper.eq("type",type);
            }
            List<RoomTypeImage> list = roomTypeImageService.list(queryWrapper);
            if(list!=null&&list.size()>0){
                List<String> srcList = new ArrayList<>();
                for(RoomTypeImage item : list){
                    srcList.add(item.getImage());
                }
                return Result.success(srcList);
            }
            return Result.error("图片列表为空");
        }catch (Exception e){
            return Result.error(e.getMessage());
        }
    }

    @PostMapping("/add")
    @ApiOperation("为房间类型添加图片,入参：(房间类型id，图片url)")
    public Result addImage(Long roomTypeId,String imageUrl,Long type){
        try {

            User user = UserUtil.getUser();

            RoomTypeImage typeImage = new RoomTypeImage();
            Date date = new Date();
            if(user!=null){
                typeImage.setCreateId(user.getId());
            }else{
                typeImage.setCreateId(0L);
            }
            typeImage.setRoomTypeId(roomTypeId);
            typeImage.setCreateTime(date);
            typeImage.setImage(imageUrl);
            typeImage.setType(type);
            roomTypeImageService.save(typeImage);
            return Result.success("保存成功");
        }catch (Exception e){
            return Result.error(e.getMessage());
        }
    }



    @PostMapping("/remove")
    @ApiOperation("通过图片ID删除图片")
    public Result removeImage(Long id){
        try {
            roomTypeImageService.removeById(id);
            return Result.success("删除I+ 成功");
        }catch (Exception e){
            return Result.error(e.getMessage());
        }
    }
}

